package tcpFirmware.controller;

import java.io.IOException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import tcpFirmware.ResponseBody;
import tcpFirmware.Routes;
import tcpFirmware.Type;
import tcpFirmware.model.DBHelper;
import tcpFirmware.model.Serial;

public class OrdersController {
	@Routes(route = "order")
	public void order(Socket socket, HashMap<String,String> head,HashMap<String,String> form) {
		String username=head.get("username");
		ArrayList<HashMap<String, String>> list=new ArrayList<>();

		Connection connection = new DBHelper().getConnection();
		try {
			Statement statement =connection.createStatement();
			ResultSet rs = statement.executeQuery("select* from orders where username='"+username+"'");
			while(rs.next()) {
				HashMap<String, String> hm=new HashMap<>();
				hm.put("oNum", rs.getString("ONum"));
				hm.put("mvName", rs.getString("name"));
				hm.put("session", rs.getString("session"));
				hm.put("center", rs.getString("centerNum"));
				hm.put("seat", rs.getString("seat"));
				hm.put("price", rs.getString("price"));
				list.add(hm);
			}
			byte[] bt=Serial.objectToBytes(list);

			ResponseBody responseBody=new ResponseBody(socket);
			responseBody.setType(Type.json.type);
			responseBody.sendResponse(bt,"notAFile");
		} catch (SQLException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Routes(route = "newOrder")
	public void newOrder(Socket socket, HashMap<String,String> head,HashMap<String,String> form){
		Connection connection = new DBHelper().getConnection();
		String username = form.get("username"),center=form.get("center"),price=form.get("price");
		String name = form.get("name"),session = form.get("session"),seat=form.get("seat");
		try {
			Statement statement1 = connection.createStatement();
			ResultSet rs=statement1.executeQuery("select thebalance from Users where username='"+username+"'");
			rs.next();
			float f=rs.getFloat("thebalance");
			ResponseBody responseBody=new ResponseBody(socket);
			responseBody.setType(Type.text.type).setRoute("newOrder");
			if(f>=Float.parseFloat(price)){
				Statement statement=connection.createStatement();
				int i=statement.executeUpdate("insert into orders values('"+username+"','"+name+"','"+session+"','"+center+"','"+price+"','"+seat+"')");
				statement.executeUpdate("update Users set thebalance = thebalance-"+Float.parseFloat(price)+"where username='"+username+"'");
				if(i==1){
					responseBody.setData("data","购票成功");
				}else {
					responseBody.setData("data","购票失败");
				}
			}
			else {
				responseBody.setData("data","余额不足");
			}
			responseBody.sendResponse();
		} catch (SQLException | IOException throwables) {
			throwables.printStackTrace();
		}
	}


	@Routes(route = "refreshSeat")
	public void refresh(Socket socket, HashMap<String,String> head,HashMap<String,String> form){
		String name = form.get("name"),session=form.get("session"),center=form.get("center");
		Connection connection = new DBHelper().getConnection();
		try {
			ArrayList<Integer> integers = new ArrayList<>();
			Statement statement = connection.createStatement();
			ResultSet rs=statement.executeQuery("select seat from orders where session='"+session+"' and centerNum='"+center+"' and name='"+name+"'");
			while (rs.next()){
				integers.add(Integer.parseInt(rs.getString("seat")));
			}
			ResponseBody responseBody = new ResponseBody(socket);
			responseBody.setType(Type.json.type).setRoute("refresh");
			responseBody.sendResponse(Serial.objectToBytes(integers),"notAFile");
		} catch (SQLException | IOException throwables) {
			throwables.printStackTrace();
		}
	}
}
